
package classes;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
//import java.io.*;
//import sun.audio.*;

public class AddPatientPanel extends JPanel {
    
    Color error_color = Color.PINK;
    Color default_color = Color.WHITE;

    JLabel lblAdd = new JLabel("Add Patient");
    JLabel lblLastName = new JLabel("Last Name:"); 
    JLabel lblFirstName = new JLabel("First Name:");
    JLabel lblMiddleName = new JLabel("Middle Innitial:");
    JLabel lblAddress = new JLabel("Address:");
    JLabel lblAge = new JLabel("Age:");
    JLabel lblGender = new JLabel("Gender:");
    JLabel lblContactNo = new JLabel("Contact Number:");
   
    JTextField txtLastName = new JTextField();
    JTextField txtFirstName = new JTextField();
    JTextField txtMiddleName = new JTextField();
    JTextField txtAge = new JTextField();
    JTextField txtContactNo = new JTextField();
    
    JTextArea txtAddress = new JTextArea();
    JScrollPane scrollAddress = new JScrollPane(txtAddress);
    
    String[] Genders = {"Male", "Female"};
    
    JComboBox selectGender = new JComboBox(Genders);
    
    JButton btnSubmit = new JButton("Submit", new ImageIcon(getClass().getResource("/images/ok.png")));
    JButton btnCancel = new JButton("Cancel", new ImageIcon(getClass().getResource("/images/cancel.png")));
    
    PatientManagement patientController = new PatientManagement();
    
    public AddPatientPanel()
    {
        //panel layout
        setLayout(null);
        setSize(500,500);
        setVisible(false);
        setOpaque(false);
        
            //add to panel
        add(lblAdd);
        add(lblLastName); 
        add(lblFirstName);
        add(lblMiddleName);
        add(lblAddress);
        add(lblAge);
        add(lblGender);
        add(lblContactNo);
        add(txtLastName);
        add(txtFirstName);
        add(txtMiddleName);
        add(txtAge);
        add(scrollAddress);
        add(txtContactNo);
        add(selectGender);
        add(btnSubmit);
        add(btnCancel);
        
        //set font
        lblAdd.setFont(new Font("Century Gothic", Font.BOLD, 16));
        lblFirstName.setFont(new Font("Century Gothic", Font.BOLD, 14));
        lblLastName.setFont(new Font("Century Gothic", Font.BOLD, 14));
        lblMiddleName.setFont(new Font("Century Gothic", Font.BOLD, 14));
        lblAddress.setFont(new Font("Century Gothic", Font.BOLD, 14));
        lblAge.setFont(new Font("Century Gothic", Font.BOLD, 14));
        lblGender.setFont(new Font("Century Gothic", Font.BOLD, 14));
        lblContactNo.setFont(new Font("Century Gothic", Font.BOLD, 14));
        txtLastName.setFont(new Font("Century Gothic", Font.PLAIN, 14));
        txtFirstName.setFont(new Font("Century Gothic", Font.PLAIN, 14));
        txtMiddleName.setFont(new Font("Century Gothic", Font.PLAIN, 14)); 
        txtAge.setFont(new Font("Century Gothic", Font.PLAIN, 14)); 
        txtAddress.setFont(new Font("Century Gothic", Font.PLAIN, 14)); 
        txtContactNo.setFont(new Font("Century Gothic", Font.PLAIN, 14)); 
        selectGender.setFont(new Font("Century Gothic", Font.PLAIN, 14));  
        btnCancel.setFont(new Font("Century Gothic", Font.BOLD, 14));
        btnSubmit.setFont(new Font("Century Gothic", Font.BOLD, 14));
      
        
       //setBounds
         lblAdd.setBounds(20,20,150,30);
         
         lblLastName.setBounds(20,60,150,30);
         txtLastName.setBounds(20,85,150,30);
         lblFirstName.setBounds(180,60,150,30);
         txtFirstName.setBounds(180,85,150,30);
         lblMiddleName.setBounds(340,60,150,30);
         txtMiddleName.setBounds(340,85,100,30);
         
         lblAddress.setBounds(20,130,150,30);
         scrollAddress.setBounds(20,155,200,80);
         scrollAddress.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
         txtAddress.setLineWrap(true);
         
         
         lblAge.setBounds(20,240,150,30);
         txtAge.setBounds(20,265,50,30);
         
         
         lblGender.setBounds(20,300,150,30);
         selectGender.setBounds(20,325,150,30);
         
         lblContactNo.setBounds(20,360,150,30);
         txtContactNo.setBounds(20,385,150,30);
         
         btnSubmit.setBounds(20, 450, 105, 30);
         btnCancel.setBounds(375, 450, 105, 30);
         
        btnSubmit.addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {       
             check();                  
            }
        }
        );
         
         //mouse listeners for the text fields
        txtLastName.addMouseListener( new MouseListener() {
             public void mousePressed(MouseEvent e) {
                txtLastName.setBackground(default_color);
             }
             public void mouseClicked(MouseEvent e) {}
             public void mouseReleased(MouseEvent e) {}
             public void mouseEntered(MouseEvent e) {}
             public void mouseExited(MouseEvent e) {}
         });
        
   
         txtFirstName.addMouseListener( new MouseListener() {
             public void mousePressed(MouseEvent e) {
                 txtFirstName.setBackground(default_color);
             }
             public void mouseClicked(MouseEvent e) {}
             public void mouseReleased(MouseEvent e) {}
             public void mouseEntered(MouseEvent e) {}
             public void mouseExited(MouseEvent e) {}
         });
         
         txtMiddleName.addMouseListener( new MouseListener() {
             public void mousePressed(MouseEvent e) {
                 txtMiddleName.setBackground(default_color);
             }
             public void mouseClicked(MouseEvent e) {}
             public void mouseReleased(MouseEvent e) {}
             public void mouseEntered(MouseEvent e) {}
             public void mouseExited(MouseEvent e) {}
         });
         
         txtAddress.addMouseListener( new MouseListener() {
             public void mousePressed(MouseEvent e) {
                 txtAddress.setBackground(default_color);
             }
             public void mouseClicked(MouseEvent e) {}
             public void mouseReleased(MouseEvent e) {}
             public void mouseEntered(MouseEvent e) {}
             public void mouseExited(MouseEvent e) {}
         });
         
         txtAge.addMouseListener( new MouseListener() {
             public void mousePressed(MouseEvent e) {
                 txtAge.setBackground(default_color);
             }
             public void mouseClicked(MouseEvent e) {}
             public void mouseReleased(MouseEvent e) {}
             public void mouseEntered(MouseEvent e) {}
             public void mouseExited(MouseEvent e) {}
         });
         
         txtContactNo.addMouseListener( new MouseListener() {
             public void mousePressed(MouseEvent e) {
                txtContactNo.setBackground(default_color);
             }
             public void mouseClicked(MouseEvent e) {}
             public void mouseReleased(MouseEvent e) {}
             public void mouseEntered(MouseEvent e) {}
             public void mouseExited(MouseEvent e) {}
         });
         
    }
   
    
    public void resetComp()
    {
        txtLastName.setText(null);
        txtFirstName.setText(null);
        txtMiddleName.setText(null);
        txtAge.setText(null);
        txtContactNo.setText(null);
        txtAddress.setText(null); 
        selectGender.setSelectedIndex(0);
    }
    
    //method for checking user inputs
    public void check() {
        String lname = txtLastName.getText();
        String fname = txtFirstName.getText();
        String mname = txtMiddleName.getText();
        String age = txtAge.getText();
        String contact = txtContactNo.getText();
        String addrs = txtAddress.getText();
        String gender = selectGender.getSelectedItem().toString();
        boolean error = false;
    
        //check if the text fields are empty
        if(lname.length() <= 0 || lname.length() >= 30) {
            txtLastName.setBackground(error_color);
            error = true;
        }
        else {
            txtLastName.setBackground(default_color);
        }
        
        if(fname.length() <= 0  || fname.length() >= 30) {
            txtFirstName.setBackground(error_color);
            error = true;
        }
        else {
            txtFirstName.setBackground(default_color);
        }
        
        if(mname.length() <= 0  || mname.length() >= 30) {
            txtMiddleName.setBackground(error_color);
            error = true;
        }
        else {
            txtMiddleName.setBackground(default_color);
        }
        
        if(addrs.length() <= 0  || addrs.length() >= 50) {
            txtAddress.setBackground(error_color);
            error = true;
        }
        else {
            txtAddress.setBackground(default_color);
        }
        
        try {
            if(age.length() <= 0 || Integer.parseInt(age) <= 0 || age.length() > 3) {
                txtAge.setBackground(error_color);
                error = true;
            }
            else {
                txtAge.setBackground(default_color);
            }
        }
        catch(Exception e)
        {    
           txtAge.setBackground(error_color);
            error = true;
        }
        
        try {
            if(contact.length() <= 0 || Double.parseDouble(contact) <= 0 || contact.length()>= 15) {
                txtContactNo.setBackground(error_color);
                error = true;
            }
            else {
                txtContactNo.setBackground(default_color);
            }
        }
        catch(Exception e)
        {
            txtContactNo.setBackground(error_color);
            error = true;
        }
             
        if(error == true)
        {

            JOptionPane.showMessageDialog(this, "Please be sure to fill up the form correctly", "Error Message", JOptionPane.ERROR_MESSAGE);    
        }
        else if(error == false) {
            
        if(patientController.checkPatient(lname,fname,mname) == true) {
            
            JOptionPane.showMessageDialog(this, "Patient is already in the database.", "Error Message", JOptionPane.ERROR_MESSAGE);
            resetComp();
        }
        else{
            int response = JOptionPane.showConfirmDialog(null, "Are you sure?", "Confirm",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.YES_OPTION) {
                   
            //database integration
            Patient newPatient = new Patient(); 
            newPatient.setLastName(lname);
            newPatient.setFirstName(fname);
            newPatient.setMiddleName(mname);
            newPatient.setAge(age);
            newPatient.setContactNo(contact);
            newPatient.setAddress(addrs);
            newPatient.setGender(gender);
            
            patientController.AddPatient(newPatient);
            
            JOptionPane.showMessageDialog(this, "The patient was added successfully.", "Message", JOptionPane.INFORMATION_MESSAGE);
            resetComp();
            resetBG();
            hideAddPanel();
            }
        }
        }
        
        
    }
      
    private void hideAddPanel()
    {
         Main.frame.darkBox2.setVisible(false);
         Main.frame.lightBox2.setVisible(false);
         Main.frame.addPatient.setVisible(false);
         Main.frame.managePatient.setPatientTable();
    }
    
    public void resetBG() {
        txtLastName.setBackground(default_color);
        txtFirstName.setBackground(default_color);
        txtMiddleName.setBackground(default_color);
        txtAge.setBackground(default_color);
        txtContactNo.setBackground(default_color);
        txtAddress.setBackground(default_color);
    }

}
